package com.itic.codeGen.codeGenerator.util;

import java.io.File;
import java.util.StringTokenizer;

import org.mybatis.generator.exception.ShellException;

public class CommonUtils {
    // file extension
    public static final String JAVA_FILE = ".java";
    public static final String XML_FILE = ".xml";
    public static final String PKG_SPLITER = ".";
    // base class notation
    public static final String BASE_MAPPER = "Mapper";
    public static final String BASE_EXAMPLE = "Example";
    public static final String BASE_IMPL = "BaseDAOImpl";
    /**
     * 
     * @author idong
     * 
     */
    public enum FILE_TYPE {
	CONTROLLER("Controller", "controller", 2), SERVICE("Service", "service", 2), DAO(
	        "Dao", "dao", 1), IMPL("DaoImpl", "dao.impl", 1), XML(
	        "_SqlMap", "dao.xml", 1);
	// type and package final layer name
	private String type, packg;
	// cut point for formatting the target package name into destination
	// package name
	private int cutPoint;

	private FILE_TYPE(String type, String packg, int cutPoint) {
	    this.type = type;
	    this.packg = packg;
	    this.cutPoint = cutPoint;
	}

	public String getType() {
	    return type;
	}

	public String getPack() {
	    return packg;
	}

	public int getCutPoint() {
	    return cutPoint;
	}
    }

    // package formatter

    /**
     * Add on line indent
     * 
     * @param sb
     * @param indentLevel
     * @return
     */
    public static StringBuilder javaIndent(StringBuilder sb, int indentLevel) {
	for (int i = 0; i < indentLevel; i++) {
	    sb.append("    "); //$NON-NLS-1$
	}
	return sb;
    }

    /**
     * Add on new line
     * 
     * @param sb
     * @return
     */
    public static StringBuilder newLine(StringBuilder sb) {
	String ls = System.getProperty("line.separator"); //$NON-NLS-1$
	if (ls == null) {
	    ls = "\n"; //$NON-NLS-1$
	}
	sb.append(ls);
	return sb;
    }

    /**
     * Output package
     * 
     * @param packageName
     * @param file_type
     * @return
     */
    public static String packageFomatter(String packageName, FILE_TYPE file_type) {
	// Set cut point
	int cutPoint = file_type.getCutPoint();
	// Set tail
	String tailString = file_type.getPack();
	// output
	StringBuilder sb = new StringBuilder();
	// split
	StringTokenizer st = new StringTokenizer(packageName, PKG_SPLITER);
	// cut point
	int tokenSize = st.countTokens() - cutPoint;
	// format
	int i = 0;
	while (i < tokenSize) {
	    sb.append(st.nextToken());
	    sb.append(PKG_SPLITER);
	    i++;
	}
	// add on
	sb.append(tailString);
	return sb.toString();
    }

    /**
     * output directory path
     * 
     * @param targetProject
     * @param targetPackage
     * @return
     * @throws ShellException
     */
    public static File getDirectory(String targetProject, String targetPackage) {
	// locate file path
	File project = new File(targetProject);
	if (!project.isDirectory()) {
	    System.out.println("check path, it is not a directory!!");
	}
	// generate package folder
	StringBuilder sb = new StringBuilder();
	StringTokenizer st = new StringTokenizer(targetPackage, PKG_SPLITER); //$NON-NLS-1$
	while (st.hasMoreTokens()) {
	    sb.append(st.nextToken());
	    sb.append(File.separatorChar);
	}
	File directory = new File(project, sb.toString());
	if (!directory.isDirectory()) {
	    boolean rc = directory.mkdirs();
	    if (!rc) {
		System.out.println("Mk dir error,  check write permission");
	    }
	}
	return directory;
    }

    /**
     * String test
     * 
     * @param s
     * @return
     */
    public static boolean testString(String s) {
	if (null != s && !"".equals(s)) {
	    return true;
	}
	return false;
    }

    /**
     * 
     * @param s
     * @return
     */
    public static String lowerFirst(String s) {
	if (Character.isLowerCase(s.charAt(0))) {
	    return s;
	}
	else {
	    StringBuilder sb = new StringBuilder();
	    sb.append(Character.toLowerCase(s.charAt(0)))
		    .append(s.substring(1));
	    return sb.toString();
	}
    }

    /**
     * 
     * @param s
     * @return
     */
    public static String getClzName(String s) {
	// split by '.'
	String[] sArray = s.split("\\.");
	int length = sArray.length;
	if (length < 1) {
	    return s;
	}
	else {
	    return sArray[length - 1];
	}
    }
}
